#!/usr/bin/env python
##
#  Project: Kelindo - Unsupervised Music Organizer  
#  Author: Nicola Bicocchi <nicola.bicocchi@gmail.com>, Nicolo' Torreggiani <nicolo.torreggiani@gmail.com>, Gianluca Puglia <gianluca.puglia@gmail.com>, Matteo Senardi <pualien@gmail.com>
#  Copyright: 2013 Nicola Bicocchi, Nicolo' Torreggiani, Gianluca Puglia, Matteo Senardi
#  License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

import hashlib

class Album:
    
    def __init__(self, dir):
        self.dir = dir
        self.new_dir = dir
        self.tracks = []
        self.avg_bitrate = 0.0
        self.album_hash = None
        
        self.status = None
        self.distance = 0.0
        self.best_candidate = None
        self.finalize = False
        
        self.format = None
        self.new_format = None
        self.need_conversion = False
        return
    
    def __repr__(self):        
        outList = []
        outList.append('\n[' + str(self.status) + '][' + str(self.finalize) + '] ' + self.dir + ' ' + str(self.avg_bitrate))
        for track in self.tracks:
            outList.append(track.__repr__())
        return '\n'.join(outList)    
    
    def get_durations(self):
        durations = []        
        for track in self.tracks:
            durations.append(track['tracklength'])
        return durations
    
    def get_track_number(self):
        return len(self.tracks)
    
    def get_track(self, track_number):
        for track in self.tracks:
            if track['trackposition'] == track_number:
                return track
            
    def get_best_candidate_track(self, track_number):
        for track in self.best_candidate:
            # cast to int is needed because musicbrainz returns unicode
            if int(track['trackposition']) == track_number:
                return track
        
    def create_hash(self):
        m = hashlib.md5()
        m.update(self.best_candidate[0]['albumauthors'] + self.best_candidate[0]['albumtitle'] + self.best_candidate[0]['trackmedium'])
        self.album_hash = m.digest()
